/*************************************************************************
	> File Name: P1439.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-04-02 11:45:40
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int binary_search(vector<int> &len, int n, int val) {
    int head = 0, tail = n, mid;
    while (head < tail) {
        mid = (head + tail + 1) >> 1;
        if (len[mid] < val) head = mid;
        else tail = mid - 1;
    }
    return head;
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1), mp(n + 1);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        mp[a[i]] = i;
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", &b[i]);
    }
    vector<int> len(n + 1), dp(n + 1);
    len[0] = INT32_MIN;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = binary_search(len, ans, mp[b[i]]) + 1;
        len[dp[i]] = mp[b[i]];
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
    return 0;
}
